package com.meilai.project.controller.web.personnel.onTurn;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.personnel.onTurn.RegularApplyCheckDTO;
import com.meilai.project.dto.personnel.onTurn.RegularApplyDTO;
import com.meilai.project.dto.personnel.onTurn.RegularApplyListDTO;
import com.meilai.project.service.personnel.onTurn.RegularApplyService;
import com.meilai.project.vo.personnel.onTurn.RegularApplyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author xchen
 * @since 2022-01-23
 */
@Api(value = "入编申请controller", tags = "入编申请")
@RestController
@RequestMapping("api/web/regular_apply")
public class RegularApplyController {

    @Autowired
    private RegularApplyService service;

    @GetMapping("list")
    @ApiOperation(value = "列表分页")
    public ApiResult<Page<RegularApplyVO>> list(@ApiParam("form") RegularApplyListDTO req) {
        Page<RegularApplyVO> regularApplyVOPage = service.selectPageList(req);
        return ApiResult.ok(regularApplyVOPage);
    }

    @GetMapping("detail")
    @ApiOperation(value = "单据明细")
    public ApiResult<RegularApplyVO> detail(
            @ApiParam("id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam("roster_id") @RequestParam(value = "roster_id", required = false) Long roster_id
    ) {
        return ApiResult.ok(service.getOneById(id, roster_id));
    }

    @PostMapping("save")
    @ApiOperation("保存")
    public ApiResult<Boolean> save(@ApiParam("json") @RequestBody RegularApplyDTO req) {
        try {
            if (service.save(req)) {
                return ApiResult.ok("新增成功");
            } else {
                return ApiResult.err("新增失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("begin")
    @ApiOperation("提交")
    public ApiResult<Boolean> begin(@ApiParam("json") @RequestBody RegularApplyDTO req) {
        try {
            if (service.begin(req)) {
                return ApiResult.ok("提交成功");
            } else {
                return ApiResult.err("提交失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("pass")
    @ApiOperation("通过")
    public ApiResult<Boolean> pass(@ApiParam("json") @RequestBody RegularApplyCheckDTO req) {
        try {
            if (service.pass(req)) {
                return ApiResult.ok("通过成功");
            } else {
                return ApiResult.err("通过失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("return_back")
    @ApiOperation("驳回")
    public ApiResult<Boolean> returnBack(@ApiParam("json") @RequestBody RegularApplyCheckDTO req) {
        try {
            if (service.returnBack(req)) {
                return ApiResult.ok("驳回成功");
            } else {
                return ApiResult.err("驳回失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "逻辑删除")
    public ApiResult<Boolean> delete(@RequestParam("id") @ApiParam("id") Long id) {
        try {
            if (service.delete(id)) {
                return ApiResult.ok("删除成功");
            } else {
                return ApiResult.err("删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("actual_delete")
    @ApiOperation(value = "物理删除")
    public ApiResult<Boolean> actualDelete(@RequestParam("id") @ApiParam("id") Long id) {
        try {
            if (service.actualDelete(id)) {
                return ApiResult.ok("彻底删除成功");
            } else {
                return ApiResult.err("彻底删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }
}
